﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autofac;
using DevExpress.XtraGrid.Columns;
using WinformDevelopFrame.Core.Extend;
using DevExpress.XtraTab;
using WinformDevelopFrame.Core.Form;
using DevExpress.XtraTreeList;
using SqlSugar.Extensions;
using WinformDevelopFrame.Common;
using SqlSugar;
using WinformDevelopFrame.Entity;

namespace WinformDevelopFrame.UI
{
    public partial class BaseForm : DevExpress.XtraEditors.XtraForm
    {
        public FormStatus FormStatus=FormStatus.First;
        public int ID = 0;  // 主键
        public List<string> ReadOnlyList = new();
        public Dictionary<string, string> fileds = new Dictionary<string, string>();
        public List<SimpleButton> btns = new();
        public BaseForm()
        {
            InitializeComponent();
        }

        private void BaseForm_Load(object sender, EventArgs e)
        {
            SetColumns(fileds);
            SetGridData();
            SetToolBtn();
            gridView1.BestFitColumns();
            InitToolBtnstatus();
            Form_Load();
        }

        public virtual void Form_Load()
        {

        }
        public void SetToolBtn()
        {
            btns.ForEach(p =>
            {
                var bs = AppInfo.Menus.Where(p => p.PId == this.Tag.ObjToInt()).Select(p=>p.MenuCode);
                if (bs.Contains(p.Name))
                {
                    flowLayoutPanel1.Controls.Add(p);
                    p.Click += ((sender, args) =>
                    {
                        var model = new sysuserlog()
                        {
                            IP = System.Net.Dns.GetHostEntry(Environment.MachineName).AddressList[0].ToString(),
                            UserId = AppInfo.User.Id,
                            Form = this.Text,
                            Action = p.Text,
                            ViewTime = DateTime.Now
                        };
                        AppInfo.Container.Resolve<ISqlSugarClient>().Insertable(model).ExecuteCommand();
                        LogHelper.Info($"IP:{model.IP},用户:{AppInfo.User.UserName},界面:{model.Form},操作:{model.Action}");
                        switch (p.Name)
                        {
                            case "btnAdd": btnAdd_Click(sender, args); break;
                            case "btnEdit": btnEdit_Click(sender, args); break;
                            case "btnSave": btnSave_Click(sender, args); break;
                            case "btnDel": btnDel_Click(sender, args); break;
                            case "btnCancel": btnCancel_Click(sender, args); break;
                            case "btnClose": btnClose_Click(sender, args); break;
                            default: SetToolBtnFunction(p);break;
                        }
                    });
                }
            });
        }

        public virtual void SetToolBtnFunction(SimpleButton btn)
        {
        }
        /// <summary>
        /// 设置数据列表列
        /// </summary>
        public virtual void SetColumns(Dictionary<string, string> Field_ColumnName)
        {
            foreach (var keyValuePair in Field_ColumnName)
            {
                GridColumn column = new GridColumn();
                column.FieldName=keyValuePair.Key;
                column.Caption=keyValuePair.Value;
                column.Visible = true;
                gridView1.Columns.Add(column);
            }
        }
        /// <summary>
        /// 设置数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listData"></param>
        public virtual void SetGridData()
        {
            
        }

        private void BaseForm_FormClosing(object sender, FormClosingEventArgs e)
        {
           
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormStatus = FormStatus.Add;
            InitToolBtnstatus();
            xtraTabControl1.SelectedTabPage = xtraTabPage2;
            ClearControlValue(this);
            ID = 0;
            AddFunction();
        }

        public virtual void AddFunction()
        {
            
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            FormStatus = FormStatus.Edit;
            InitToolBtnstatus();
            EditFunction();
        }
        public virtual void EditFunction()
        {

        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveFunction())
            {
                "保存成功".ShowTips();
                SetGridData();
                FormStatus = FormStatus.Save;
                InitToolBtnstatus();
            }
            else
            {
                "保存失败".ShowTips();
            }
        }
        public virtual bool SaveFunction()
        {
            return false;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (DeleteFunction())
            {
                FormStatus = FormStatus.Del;
                InitToolBtnstatus();
                SetGridData();
                ClearControlValue(this);
                ID = 0;
            }
        }
        public virtual bool DeleteFunction()
        {
            return false;
        }
        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (FormStatus == FormStatus.Add)
                FormStatus = FormStatus.AddCancel;
            else
            {
                FormStatus = FormStatus.EditCancel;
            }
            InitToolBtnstatus();
            CancelFunction();
        }
        public virtual bool CancelFunction()
        {
            return false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            xtraTabControl1.SelectedTabPage = xtraTabPage2;
            this.ModelDataToControl(gridView1.GetRow(gridView1.FocusedRowHandle));
            FormStatus = FormStatus.View;
            InitToolBtnstatus();
            GridControlDoubleClickFunction();
        }

        public virtual void GridControlDoubleClickFunction()
        {

        }
        public void ClearControlValue(Control ctrl)
        {
            ClearSinglelValue(ctrl);
            if (ctrl.Controls.Count > 0)
            {
                foreach (System.Windows.Forms.Control control in ctrl.Controls)
                {
                    ClearControlValue(control);
                }
            }
        }
        public void ClearSinglelValue(Control ctrl)
        {
            if (!ReadOnlyList.Contains(ctrl.Name))
            {
                switch (ctrl.GetType().Name)
                {
                    case "TextEdit":
                    case "MemoEdit":
                        (ctrl).Text = "";
                        break;
                    case "LookUpEdit":
                        ((LookUpEdit)ctrl).EditValue = "";
                        ((LookUpEdit)ctrl).Properties.NullText = "";
                        break;
                    case "CheckEdit":
                        ((CheckEdit)ctrl).Checked = false;
                        break;

                    case "ComboBoxEdit":
                        ((ComboBoxEdit)ctrl).EditValue = "";
                        break;
                    case "TreeListLookUpEdit":
                        ((TreeListLookUpEdit)ctrl).EditValue = "";
                        ((TreeListLookUpEdit)ctrl).Properties.NullText = "";
                        break;
                    case "CheckedComboBoxEdit":
                        ((CheckedComboBoxEdit)ctrl).EditValue = "";
                        break;
                    case "DateEdit":
                        ((DateEdit)ctrl).EditValue = null;
                        break;
                    case "SearchLookUpEdit":
                        ((SearchLookUpEdit)ctrl).EditValue = "";
                        ((SearchLookUpEdit)ctrl).Properties.NullText = "";
                        break;
                }
            }
           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            var p = (XtraTabPage)this.Parent;
            if (p != null)
            {
                var tab = (XtraTabControl)p.Parent;
                tab.TabPages.Remove(p);
                tab.SelectedTabPageIndex = tab.TabPages.Count - 1;
            }
            CloseFunction();
            this.Close();
        }

        public virtual void CloseFunction()
        {

        }
        public virtual void InitToolBtnStatusFunction(FormStatus status,SimpleButton btn)
        {

        }
        public void InitToolBtnstatus()
        {
            switch (FormStatus)
            {
                case Core.Form.FormStatus.First:
                    {
                        foreach (var button in flowLayoutPanel1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnSearch" || btn.Name == "btnClose")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                                InitToolBtnStatusFunction(FormStatus, btn);
                            }
                        }
                    }; break;
                case Core.Form.FormStatus.Save:
                {
                    foreach (var button in flowLayoutPanel1.Controls)
                    {
                        if (button.GetType().Name == "SimpleButton")
                        {
                            var btn = (SimpleButton)button;
                            if (btn.Name == "btnAdd" || btn.Name == "btnDel" || btn.Name == "btnEdit" || btn.Name == "btnSearch" || btn.Name == "btnClose")
                            {
                                btn.Enabled = true;
                            }
                            else
                            {
                                btn.Enabled = false;
                            }
                            InitToolBtnStatusFunction(FormStatus, btn);
                        }
                    }
                    xtraTabPage1.PageEnabled = true;
                }; break;
                case FormStatus.View:
                    {
                        foreach (var button in flowLayoutPanel1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnDel" || btn.Name == "btnEdit" || btn.Name == "btnSearch" || btn.Name == "btnClose")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                                InitToolBtnStatusFunction(FormStatus, btn);
                            }
                        }
                    }; break;
                case FormStatus.Add:
                    {
                        foreach (var button in flowLayoutPanel1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnCancel" || btn.Name == "btnSave" || btn.Name == "btnClose")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                                InitToolBtnStatusFunction(FormStatus, btn);
                            }
                        }

                        xtraTabPage1.PageEnabled = false;
                    }; break;
                case FormStatus.Del:
                    {
                        foreach (var button in flowLayoutPanel1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnSearch" || btn.Name == "btnClose")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                                InitToolBtnStatusFunction(FormStatus, btn);
                            }
                        }
                    }; break;
                case FormStatus.Edit:
                    {
                        foreach (var button in flowLayoutPanel1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnCancel" || btn.Name == "btnSave" || btn.Name == "btnClose")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                                InitToolBtnStatusFunction(FormStatus, btn);
                            }
                        }
                        xtraTabPage1.PageEnabled = false;
                    }; break;
                case FormStatus.AddCancel:
                    {
                        foreach (var button in flowLayoutPanel1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd"   || btn.Name == "btnClose")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                                InitToolBtnStatusFunction(FormStatus, btn);
                            }
                        }
                        xtraTabPage1.PageEnabled=true;
                    }; break;
                case FormStatus.EditCancel:
                {
                    foreach (var button in flowLayoutPanel1.Controls)
                    {
                        if (button.GetType().Name == "SimpleButton")
                        {
                            var btn = (SimpleButton)button;
                            if (btn.Name == "btnAdd" || btn.Name == "btnDel" || btn.Name == "btnClose" || btn.Name == "btnEdit")
                            {
                                btn.Enabled = true;
                            }
                            else
                            {
                                btn.Enabled = false;
                            }
                            InitToolBtnStatusFunction(FormStatus, btn);
                        }
                    }
                    xtraTabPage1.PageEnabled = true;
                }; break;
            }
            
        }
    }
}